package com.tsingteng.cosfun.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.ThirdBindBean;
import com.tsingteng.cosfun.common.Config;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.ui.costar.PreViewActivity;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.StretchScrollView;
import com.tsingteng.cosfun.widget.TopActionBar;
import com.tsingteng.cosfun.widget.dialog.ContentDialog;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * author :zhikuan
 * Date:18-8-12
 */
public class AccountManageFragment extends BasePresenterFragment<AccountManagePresenter, AccountMangeContract.IAccountMangeView> implements AccountMangeContract.IAccountMangeView {

    @BindView(R.id.id_account)
    TextView idAccount;
    @BindView(R.id.prefid_id)
    TextView mId;
    @BindView(R.id.phone_num)
    TextView phoneNum;
    @BindView(R.id.weixin)
    TextView weixin;
    @BindView(R.id.sina)
    TextView sina;
    @BindView(R.id.qq)
    TextView qq;
    @BindView(R.id.scroll_view)
    StretchScrollView scrollView;
    @BindView(R.id.setting_topbar)
    TopActionBar settingTopbar;
    @BindView(R.id.tv_bind_mobile)
    TextView tvBindMobile;
    @BindView(R.id.tv_bind_wx)
    TextView tvBindWx;
    @BindView(R.id.tv_bind_wb)
    TextView tvBindWb;
    @BindView(R.id.tv_bind_qq)
    TextView tvBindQq;
    private ContentDialog contentDialog;
    private String type = "";

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getAccountMangeMentData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_setting_account_manage_layout;
    }

    @Override
    protected void initData(View rootView) {
        mPresenter.getAccountMangeMentData();
    }

    @Override
    protected AccountManagePresenter createPresenter() {
        return new AccountManagePresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return null;
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return null;
    }

    @Override
    protected void refresh() {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreRequested() {

    }

    @Override
    public void showThirdBindResult(ThirdBindBean thirdBindBean) {
        mPresenter.getAccountMangeMentData();
    }


    @Override
    public void showAccountMangeMent(ThirdBindBean thirdBindBean) {
        mId.setText(""+ LogonUtils.getProFildId());
        if (!TextUtils.isEmpty(thirdBindBean.getBind_mobile())) {
            tvBindMobile.setText(thirdBindBean.getBind_mobile());
            LogonUtils.setBindType(Config.BIND_MOBILE, true);
            LogonUtils.setMobile(thirdBindBean.getBind_mobile());
        } else {
            tvBindMobile.setText("未绑定");
            LogonUtils.setBindType(Config.BIND_MOBILE, false);
        }
        if (!TextUtils.isEmpty(thirdBindBean.getBing_qq())) {
            tvBindQq.setText(thirdBindBean.getBing_qq());
            LogonUtils.setBindType(Config.BIND_QQ, true);
        } else {
            tvBindQq.setText("未绑定");
            LogonUtils.setBindType(Config.BIND_QQ, false);
        }
        if (!TextUtils.isEmpty(thirdBindBean.getBing_wb())) {
            tvBindWb.setText(thirdBindBean.getBing_wb());
            LogonUtils.setBindType(Config.BIND_WB, true);
        } else {
            tvBindWb.setText("未绑定");
            LogonUtils.setBindType(Config.BIND_WB, false);
        }
        if (!TextUtils.isEmpty(thirdBindBean.getBing_wx())) {
            tvBindWx.setText(thirdBindBean.getBing_wx());
            LogonUtils.setBindType(Config.BIND_WX, true);
        } else {
            tvBindWx.setText("未绑定");
            LogonUtils.setBindType(Config.BIND_WX, false);
        }
    }

    @Override
    public void showThirdUnBindResult(Integer status) {
        mPresenter.getAccountMangeMentData();
    }

    @OnClick({R.id.id_account, R.id.phone_num, R.id.weixin, R.id.sina, R.id.qq})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_account:
                break;
            case R.id.phone_num:
                bindThird(0);
                break;
            case R.id.weixin:
                bindThird(1);
                break;
            case R.id.sina:
                bindThird(2);
                break;
            case R.id.qq:
                bindThird(3);
                break;
        }
    }

    public void bindThird(int type) {
        switch (type) {
            case 0:
                if (LogonUtils.isBindType(Config.BIND_MOBILE)) {
                    if (checkUnBind("手机")) {
                        return;
                    }
                } else {
                    Navigate.startMobile(getContext(),"");
                }
                this.type = "手机";
                break;
            case 1:
                if (LogonUtils.isBindType(Config.BIND_WX)) {
                    if (checkUnBind("微信")) {
                        return;
                    }
                } else {
                    LogonUtils.umengDeleteOauth(getActivity(), SHARE_MEDIA.WEIXIN);
                    LogonUtils.shareLoginUmeng(getActivity(), SHARE_MEDIA.WEIXIN, getOnLogonListener());
                }
                this.type = "微信";
                break;
            case 2:
                if (LogonUtils.isBindType(Config.BIND_WB)) {
                    if (checkUnBind("微博")) {
                        return;
                    }
                } else {
                    LogonUtils.umengDeleteOauth(getActivity(), SHARE_MEDIA.SINA);
                    LogonUtils.shareLoginUmeng(getActivity(), SHARE_MEDIA.SINA, getOnLogonListener());
                }
                this.type = "微博";
                break;
            case 3:
                if (LogonUtils.isBindType(Config.BIND_QQ)) {
                    if (checkUnBind("QQ")) {
                        return;
                    }
                } else {
                    LogonUtils.umengDeleteOauth(getActivity(), SHARE_MEDIA.QQ);
                    LogonUtils.shareLoginUmeng(getActivity(), SHARE_MEDIA.QQ, getOnLogonListener());
                }
                this.type = "QQ";
                break;
        }
    }

    @NonNull
    private LogonUtils.OnLogonListener getOnLogonListener() {
        return new LogonUtils.OnLogonListener() {
            @Override
            public void onStartLogon() {
                showLoading("正在启动...");
            }

            @Override
            public void onCompleteLogon(SHARE_MEDIA share_media_type, String openid, String access_token, String iconurl, String name) {
                hideLoading();
                if (share_media_type == SHARE_MEDIA.QQ) {
                    mPresenter.getThirdBindData(access_token, "3", name, iconurl, openid);
                } else if (share_media_type == SHARE_MEDIA.WEIXIN) {
                    mPresenter.getThirdBindData(access_token, "5", name, iconurl, openid);
                } else {
                    mPresenter.getThirdBindData(name, "4", name, iconurl, name);
                }
            }

            @Override
            public void onCancleLogon() {
                hideLoading();
            }

            @Override
            public void onErrorLogon(Throwable throwable) {
                hideLoading();
            }
        };
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(getActivity()).onActivityResult(requestCode, resultCode, data);
        Log.e("umengDeleteOauth", "onActivityResult: " + requestCode + "\n" + resultCode + "\n" + data);
    }

    /**
     * 校验是否可以绑定
     *
     * @return
     */
    public boolean checkUnBind(String type) {
        this.type = type;
        final boolean[] isUnbind = {false};
        int size = 0;
        if (LogonUtils.isBindType(Config.BIND_QQ)) size++;
        if (LogonUtils.isBindType(Config.BIND_WX)) size++;
        if (LogonUtils.isBindType(Config.BIND_WB)) size++;
        if (LogonUtils.isBindType(Config.BIND_MOBILE)) size++;
        if (size < 2) {
            contentDialog = new ContentDialog(getActivity());
            contentDialog.setContent("不可解绑，至少保留一个登录账号！").setAffirmContent("我知道了").setOnOneClickListener(new ContentDialog.OnOneClickListener() {
                @Override
                public void onAffirm() {

                }
            }).show();
            return true;
        }
        contentDialog = new ContentDialog(getActivity());
        contentDialog.setTitle("是否解除" + type + "绑定?");
        contentDialog.setContent("解除绑定将无法使用此" + type + "登录烤饭ID" + LogonUtils.getProFildId() + "，此" + type + "可作为新用户注册使用烤饭APP。")
                .setAffirmContentColor(R.color.red_bag_color)
                .setAffirmContent("解除").setCancelContent("取消")
                .setOnTwoClickListener(new ContentDialog.OnTwoClickListener() {
            @Override
            public void onAffirm() {
                if (type == "QQ") {
                    mPresenter.getThirdUnBind("qq");
                }
                if (type == "微信") {
                    mPresenter.getThirdUnBind("wx");
                }
                if (type == "微博") {
                    mPresenter.getThirdUnBind("wb");
                }
                if (type=="手机") {
                    Navigate.startMobile(getContext(),LogonUtils.getMobile());
                }
            }
            @Override
            public void onCancel() {
                isUnbind[0] = true;
            }
        }).show();
        return isUnbind[0];
    }

    @Override
    public void showFail(String msg) {
        if (msg.contains("绑定")) {
            contentDialog = new ContentDialog(getActivity());
            contentDialog.setTitle("此"+type+"已存在绑定关系！");
            contentDialog.setContent("请先解除本"+type+"的绑定关系，再来进行绑定操作。").setAffirmContent("确定").setOnOneClickListener(new ContentDialog.OnOneClickListener() {
                @Override
                public void onAffirm() {

                }
            }).show();
        }
    }
}
